package com.reebake.ideal.infra.controller;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reebake.ideal.mybatis.core.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.reebake.ideal.infra.entity.Region;
import com.reebake.ideal.infra.service.RegionService;
import com.reebake.ideal.infra.vo.RegionQueryParamVO;
import com.reebake.ideal.infra.vo.RegionVO;

 /**
 * 行政区划控制层
 */
@Tag(name = "行政区划对象功能接口")
@RestController
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private RegionService regionService;
    
    /** 
     * 分页查询
     *
     * @param queryParam 查询参数
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @PostMapping("page")
    public PageResult<RegionVO> page(@RequestBody RegionQueryParamVO queryParam) {
        LambdaQueryWrapper<Region> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(queryParam.getRegionCode()), Region::getRegionCode, queryParam.getRegionCode());
        queryWrapper.like(StrUtil.isNotBlank(queryParam.getRegionName()), Region::getRegionName, queryParam.getRegionName());
        queryWrapper.eq(ObjectUtil.isNotNull(queryParam.getRegionLevel()), Region::getRegionLevel, queryParam.getRegionLevel());
        queryWrapper.eq(StrUtil.isNotBlank(queryParam.getParentId()), Region::getParentId, queryParam.getParentId());
        queryWrapper.like(StrUtil.isNotBlank(queryParam.getParentName()), Region::getParentName, queryParam.getParentName());
        
        queryParam = regionService.page(queryParam, queryWrapper);
        
        return queryParam.to(RegionVO.class);
    }
    
    /** 
     * 保存或更新数据
     *
     * @param region 实例对象
     */
    @Operation(summary = "保存或更新数据")
    @PostMapping("save")
    public void save(@RequestBody RegionVO region) {
        regionService.saveOrUpdate(BeanUtil.copyProperties(region, Region.class));
    }
    
    /** 
     * 通过主键批量删除
     *
     * @param regionIds 主键
     */
    @Operation(summary = "通过主键批量删除")
    @PostMapping("remove-by-ids")
    public void removeByIds(@RequestBody List<String> regionIds) {
        regionService.removeByIds(regionIds);
    }
}